package week6;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

/**
 * @author The Setting sun
 * @create 2021-10-14 15:27
 */
public class Work4 {
    public static void main(String[] args) {
        JFrame frame = new JFrame();

        JMenuBar mb = new JMenuBar();
        JMenu m = new JMenu("文件");
        JMenuItem mi1 = new JMenuItem("新建");
        JMenuItem mi2 = new JMenuItem("删除");
        JMenuItem mi3 = new JMenuItem("查看");
        JMenuItem mi4 = new JMenuItem("退出");

        mi1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NewCreateJDeleteDialog d = new NewCreateJDeleteDialog();

                d.b1.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        File file = new File(d.text.getText());

                        try {
                            file.createNewFile();
                        } catch (IOException ioException) {
                            ioException.printStackTrace();
                        }

                        System.exit(1);
                    }
                });
            }
        });

        mi2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NewCreateJDeleteDialog d = new NewCreateJDeleteDialog();

                d.b1.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        File file = new File(d.text.getText());

                        file.delete();

                        System.exit(1);
                    }
                });
            }
        });

        mi3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ShowDialog d = new ShowDialog();

                d.b1.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        File file = new File(d.text.getText());

                        d.show.setText("最后修改时间：" + file.lastModified());
                    }
                });
            }
        });


        mb.add(m);
        m.add(mi1);
        m.add(mi2);
        m.add(mi3);
        m.addSeparator();//分隔符
        m.add(mi4);

        frame.setJMenuBar(mb);
        frame.setSize(600, 500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
    }
}

class NewCreateJDeleteDialog extends JDialog {
    JLabel label = new JLabel("输入路径：");
    JTextField text = new JTextField(45);
    JButton b1 = new JButton("确认");
    JButton b2 = new JButton("取消");

    public NewCreateJDeleteDialog() {
        super();
        JPanel panel = new JPanel(new FlowLayout());
        label.setFont(new Font("微软雅黑", Font.BOLD, 10));
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        panel.add(label);
        panel.add(text);
        panel.add(b1);
        panel.add(b2);

        add(panel);

        setSize(600, 120);
        setVisible(true);
        setLocationRelativeTo(null);
    }
}


class ShowDialog extends JDialog {
    JLabel label = new JLabel("输入路径：");
    JTextField text = new JTextField(45);
    JButton b1 = new JButton("确认");
    JButton b2 = new JButton("取消");
    JLabel show = new JLabel();

    public ShowDialog() {
        super();
        JPanel panel = new JPanel(new FlowLayout());
        label.setFont(new Font("微软雅黑", Font.BOLD, 10));
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.CENTER));

        panel.add(label);
        panel.add(text);
        panel.add(b1);
        panel.add(b2);

        panel1.add(show);

        add(panel, BorderLayout.CENTER);
        add(panel1, BorderLayout.SOUTH);

        setSize(600, 200);
        setVisible(true);
        setLocationRelativeTo(null);
    }
}